package com.nidu.demo.oauth2.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.oauth2.convertor.OAuth2CodeConvertor;
import com.nidu.demo.oauth2.gateway.OAuth2CodeGateway;
import com.nidu.demo.oauth2.mapper.OAuth2CodeMapper;
import com.nidu.demo.oauth2.mapper.dataobject.OAuth2ClientDO;
import com.nidu.demo.oauth2.mapper.dataobject.OAuth2CodeDO;
import com.nidu.demo.oauth2.model.OAuth2Code;
import com.nidu.demo.oauth2.model.OAuth2CodeCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class OAuth2CodeGatewayImpl implements OAuth2CodeGateway {

    private final OAuth2CodeMapper mapper;
    private final OAuth2CodeConvertor convertor;

    @Override
    public OAuth2Code create(OAuth2Code code) {
        OAuth2CodeDO codeDO = convertor.toDataObject(code);
        mapper.insert(codeDO);
        return convertor.toDomainObject(codeDO);
    }

    @Override
    public OAuth2Code update(OAuth2Code code) {
        OAuth2CodeDO codeDO = convertor.toDataObject(code);
        mapper.updateById(codeDO);
        return convertor.toDomainObject(codeDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public OAuth2Code getById(Long id) {
        OAuth2CodeDO codeDO = mapper.selectById(id);
        if (codeDO == null) {
            return null;
        }
        return convertor.toDomainObject(codeDO);
    }

    @Override
    public OAuth2Code getByCode(String code) {
        if (code == null) {
            return null;
        }
        OAuth2CodeDO codeDO = mapper.selectByCode(code);
        if (codeDO == null) {
            return null;
        }
        return convertor.toDomainObject(codeDO);
    }

    @Override
    public List<OAuth2Code> listByCondition(OAuth2CodeCriteria criteria) {
        LambdaQueryWrapper<OAuth2CodeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollUtil.isEmpty(criteria.getCodeIds()), OAuth2CodeDO::getId, criteria.getCodeIds());

        wrapper.eq(!StrUtil.isEmpty(criteria.getAuthorizationCode()), OAuth2CodeDO::getAuthorizationCode, criteria.getAuthorizationCode());
        wrapper.eq(criteria.getClientId() != null, OAuth2CodeDO::getClientId, criteria.getClientId());
        wrapper.eq(criteria.getUserId() != null, OAuth2CodeDO::getUserId, criteria.getUserId());

        List<OAuth2CodeDO> codeDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(codeDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(codeDOS);
    }

    @Override
    public PageResponse<OAuth2Code> pageByCondition(OAuth2CodeCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<OAuth2CodeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollUtil.isEmpty(criteria.getCodeIds()), OAuth2CodeDO::getId, criteria.getCodeIds());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(OAuth2CodeDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(OAuth2CodeDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.eq(!StrUtil.isEmpty(criteria.getAuthorizationCode()), OAuth2CodeDO::getAuthorizationCode, criteria.getAuthorizationCode());
        wrapper.eq(criteria.getClientId() != null, OAuth2CodeDO::getClientId, criteria.getClientId());
        wrapper.eq(criteria.getUserId() != null, OAuth2CodeDO::getUserId, criteria.getUserId());

        // 创建分页对象，确保使用 OAuth2CodeDO
        Page<OAuth2CodeDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }
        mapper.selectPage(page, wrapper);
        List<OAuth2Code> codes = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(codes, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }
}